package work.sajor.wechatpush.service.impl;

import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import work.sajor.wechatpush.entity.ReturnTextMessageEntity;
import work.sajor.wechatpush.service.WechatService;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    // checkSignature 方法的实现省略

    /**
     * 用户在微信公众号发送消息后, 微信服务器会发送一个 POST 请求过来
     * POST 请求中携带者用户发送的数据（XML 格式）
     * @param req 可通过该参数获得 XML 数据包的输入流
     * @return 数据的 Map 格式
     */
    @Override
    public Map<String, String> parseXmlData2Map(HttpServletRequest req) {
        HashMap<String, String> msgMap = new HashMap<>();

        try {
            ServletInputStream inputStream = req.getInputStream();
            // dom4j 用于读取 XML 文件输入流的类
            SAXReader saxReader = new SAXReader();
            // 读取 XML 文件输入流, XML 文档对象
            Document document = saxReader.read(inputStream);
            // XML 文件的根节点
            Element root = document.getRootElement();
            // 所有的子节点
            List<Element> childrenElement = root.elements();
            for (Element element : childrenElement) {
                msgMap.put(element.getName(), element.getStringValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msgMap;
    }

    /**
     *  map转成xml文件
     */
    @Override
    public String parseDataToXml(Map map) {
        String toUserName = (String)map.get("ToUserName");
        String fromUserName = (String) map.get("FromUserName");
        String content = (String) map.get("Content");
        int currentTimeMillis = (int)System.currentTimeMillis();

        ReturnTextMessageEntity re = new ReturnTextMessageEntity();
        re.setContent("接收后返回信息:"+content.toString());
        re.setCreateTime(currentTimeMillis);
        re.setFromUserName(toUserName);
        re.setMsgType("text");
        re.setToUserName(fromUserName);
        XStream xStream = new XStream();//需要XPP3库

        xStream.alias("xml",ReturnTextMessageEntity.class);
        String xml = xStream.toXML(re);
        System.out.println("返回的xml消息:"+xml);
        return xml;
    }


    public static void main(String[] args) {
        ReturnTextMessageEntity re = new ReturnTextMessageEntity();
        re.setContent("111");
        re.setCreateTime(1111);
        re.setFromUserName("1111111");
        re.setMsgType("sdf");
        re.setToUserName("dsd");
        XStream xStream = new XStream();//需要XPP3库
        //XStream xStream = new XStream(new DomDriver());//不需要XPP3库
        //XStream xStream = new XStream(new StaxDriver());//不需要XPP3库开始使用Java 6
        //XML序列化
        xStream.alias("xml",ReturnTextMessageEntity.class);
        String xml = xStream.toXML(re);
        System.out.println(xml);
    }


}